package com.vslimit.command;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: vslimit
 * Date: 12-4-3
 * Time: 上午12:41
 * To change this template use File | Settings | File Templates.
 */
public abstract class PageCommand {

    public PageCommand() {

    }


    public PageCommand(Long id, String objectStatement) {
        setId(id);
        setObjectStatement(objectStatement);
    }

    private Long id;

    private String listStatement;
    private String countStatement;
    private String objectStatement;
    private Integer pageNumber = 1;
    private Integer nextPageNumber;
    private Integer previousPageNumber;
    private Integer lastPageNumber;
    private Integer pageSize = 10;
    private Integer count = null;
    private JdbcTemplate jdbcTemplate;
    private RowMapper rowMapper;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//        JdbcTemplate _jdbcTemplate = (JdbcTemplate)
//        this.jdbcTemplate = _jdbcTemplate;
//    }

    public void setListStatement(String listStatement) {
        this.listStatement = listStatement;
    }

    public void setCountStatement(String countStatement) {
        this.countStatement = countStatement;
    }

    public void setObjectStatement(String objectStatement) {
        this.objectStatement = objectStatement;
    }


    public void setRowMapper(RowMapper rowMapper) {
        this.rowMapper = rowMapper;
    }

//    public JdbcTemplate getJdbcTemplate() {
//        return (JdbcTemplate) FuniApplicationContext.getBean("jdbcTemplate");
//    }


    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public String getListStatement() {
        return listStatement;
    }

    public RowMapper getRowMapper() {
        return rowMapper;
    }

    /**
     * 得到查询的数据数据
     *
     * @return
     */
    public List getPageList() {
        return jdbcTemplate.query(listStatement, rowMapper, pageCommandToParameter());
    }

    /**
     * 获取数据数量
     *
     * @return
     */
    public Integer getDataCount() {
//        if (count == null) {
        count = jdbcTemplate.queryForInt(countStatement, countCommandToParameter());
//        }
        return count;
    }

    public abstract List commandToParameter();

    public List getParameter() {
        List<Object> objectList = new ArrayList<Object>();
        for (Object o : commandToParameter()) {
            if (o != null) {
                objectList.add(o);
            }
        }
        return objectList;
    }

    private Object[] pageCommandToParameter() {
        List objectList = getParameter();
        objectList.add(getBegin());
        return objectList.toArray(new Object[objectList.size()]);
    }

    private Object[] countCommandToParameter() {
        List objectList = getParameter();
        return objectList.toArray(new Object[objectList.size()]);
    }

    protected Integer getBegin() {
        return (pageNumber - 1) * pageSize;
    }

    private Integer getEnd() {
        return pageNumber * pageSize >= count ? count : pageNumber * pageSize;
    }


    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getLastPageNumber() {
        lastPageNumber = (getDataCount() - 1) / pageSize + 1;
        return lastPageNumber;
    }


    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public Integer getNextPageNumber() {
        if (pageNumber >= 1 && pageNumber < getLastPageNumber()) {
            nextPageNumber = pageNumber + 1;
        } else {
            nextPageNumber = getLastPageNumber();
        }
        return nextPageNumber;
    }

    public Integer getPreviousPageNumber() {
        if (pageNumber > 1 && pageNumber <= getLastPageNumber()) {
            previousPageNumber = pageNumber - 1;
        } else {
            previousPageNumber = 1;
        }
        return previousPageNumber;
    }


    public Object load() {
        return jdbcTemplate.queryForObject(objectStatement, new Object[]{id}, rowMapper);
    }
}
